package com.yehangtech.designpattern.行为型_观察者模式.service.api;

import com.yehangtech.designpattern.行为型_观察者模式.service.event.EventManager;
import com.yehangtech.designpattern.行为型_观察者模式.service.event.MQEventListener;
import com.yehangtech.designpattern.行为型_观察者模式.service.event.MessageEventListener;

public abstract class AbstractLotteryService {
    private EventManager eventManager;

    public AbstractLotteryService() {
        eventManager = new EventManager(EventManager.EventType.MQ, EventManager.EventType.Message);
        eventManager.subscribe(EventManager.EventType.MQ, new MQEventListener());
        eventManager.subscribe(EventManager.EventType.Message, new MessageEventListener());
    }

    public String draw(String uId) {
        String lotteryResult = doDraw(uId);
        // 需要什么通知就给调⽤什么⽅法
        eventManager.notify(EventManager.EventType.MQ, lotteryResult);
        eventManager.notify(EventManager.EventType.Message, lotteryResult);
        return lotteryResult;
    }

    protected abstract String doDraw(String uId);
}
